''''全国KFC门店城市分布数量图, data form YumChina'''

from collections import Counter
import pandas as pd
from pyecharts import Geo

#数据定义
COLUMNS = ["store_name", "city", "store_numb", "longitude", "latitude"]

def load_data(file_name):

    data = pd.read_csv(file_name, names=COLUMNS)   #数据路径
    data = data.dropna()    #删除所有包含NULL的数据

    #部分省市县重命名
    data.city[(data.city == "省直辖县") & (data.latitude < 20)] = '海南省直辖县'
    data.city[(data.city == "省直辖县") & (data.latitude > 20)] = '湖北省直辖县'
    data.city[(data.city == "省(自治区)直辖县")] = '石河子直辖县'
    return data

#生成绘图数据，城市名称和门店数量的映射
def dict_generator(dataset):

    city = Counter(dataset.city)
    city_dict = dict(city)
    return city_dict


def draw_graph(city_dict):
    geo = Geo("全国各城市KFC门店数量分布图", "data from YumChina", title_pos="center", width=1600,
              height=800)
    attr, value = geo.cast(city_dict)

    #加入库内不存在的城市的坐标：
    geo.add_coordinate('黔南布依族苗族自治州', 104.919227, 25.098920)
    geo.add_coordinate('黔西南布依族苗族自治州', 104.918640, 25.097109)
    geo.add_coordinate('菏泽市', 116.109932, 34.777239)
    geo.add_coordinate('普洱市', 100.971409, 22.777727)
    geo.add_coordinate('嘉峪关市 ', 98.27, 39.80)
    geo.add_coordinate('海南省直辖县', 110.758947, 19.606585)
    geo.add_coordinate('湖北省直辖县', 113.448870, 30.368365)
    geo.add_coordinate('襄阳市', 111.642317, 32.262862)
    geo.add_coordinate('石河子直辖县', 86.069285, 44.301701)

    geo.add("", attr, value, visual_split_number=6, visual_text_color="#fff",
            symbol_size=15, is_visualmap=True, show=False)

    geo.render()

if __name__ == "__main__":

    #导入.csv文件的相对路径
    city_data = load_data("store.csv")
    city_dict = dict_generator(city_data)
    draw_graph(city_dict)
